//
//  HDNewsViewController.swift
//  Excited
//
//  Created by X140Yu on 6/13/16.
//  Copyright © 2016 X140Yu. All rights reserved.
//

import UIKit
import MJRefresh
import PKHUD

class HDNewsViewController: UITableViewController {
    var newsListArray = [HDNewsListItem]()
    var currentFetchIndex = 2

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 102
        
        
        tableView.registerNib(UINib(nibName: "HDNewsDefaultTableViewCell", bundle: nil), forCellReuseIdentifier: "NewsDefault")
        
        tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            HDNetwork.fetchNewsListWith(1, completion: { [unowned self] (newsList) in
                self.newsListArray = newsList
                self.tableView.mj_header.endRefreshing()
                self.tableView.reloadData()
                self.currentFetchIndex = 2
            }) { (message) in
                HUD.flash(HUDContentType.LabeledError(title: message, subtitle: nil), delay: 0.8)
                self.tableView.mj_header.endRefreshing()
            }
        })

        tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {
            HDNetwork.fetchNewsListWith(self.currentFetchIndex, completion: { [unowned self] (newsList) in
                if newsList.count == 0 {
                    self.tableView.mj_footer.endRefreshing()
                    return
                }
                self.newsListArray += newsList
                self.tableView.mj_footer.endRefreshing()
                self.currentFetchIndex += 1
                self.tableView.reloadData()
            }) { (message) in
                HUD.flash(HUDContentType.LabeledError(title: message, subtitle: nil), delay: 0.8)
                self.tableView.mj_footer.endRefreshing()
            }
        })

        tableView.reloadData()
        tableView.mj_header.beginRefreshing()
    }
}

extension HDNewsViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return newsListArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("NewsDefault", forIndexPath: indexPath) as! HDNewsDefaultTableViewCell
        cell.setNewsItem(newsListArray[indexPath.row])
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cell = tableView.cellForRowAtIndexPath(indexPath) as! HDNewsDefaultTableViewCell
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let newsDetailVC = HDNewsDetailViewController()
        newsDetailVC.setContentWith(cell.news!.nid!)
        navigationController?.pushViewController(newsDetailVC, animated: true)
    }
    
    
}


